/**
 *
 * @authors Your Name (you@example.org)
 * @date    2014-11-17 16:51:58
 * @version $Id$
 */
var changeCommentTable = (function() {
    var tableInit = [false, false, false];
    var TPL = '<li>\
								<div class="tbox">\
									<div>\
										<span class="img_wrap"><img src="{HeadUrl}" /></span>\
										<p>{NickName}</p>\
										<p style="align:center;">{MemberGrade}</p>\
									</div>\
									<div>\
										<p class="comment_content">{Content}</p>\
										<p>\
											<label class="comment_rate" data-rate="{Star}"></label>\
											<label class="comment_time">{AddTime} </label>\
										</p>\
                                        <p><div class="reply" {DisplayReply}>{ReplyContent}</div></p>\
									</div>\
								</div>\
							</li>';

    function load_comments(args, fn) {
        $.ajax({
            type: "POST",
            url: APP.urls.load_comments,
            data: args,
            async: true,
            success: function(res) {
                fn.call(null, res);
            },
            dataType: "json"
        });
    }

    function changeCommentTable(thi, evt, index) {
        if (1 == 1) {
            $("#list_comments").parent().html('<p class="no_comments">暂无评价</p>');
            return;
        }
        changeCommentTable.index = index;
        switch (true) {
            case (0 == index && !tableInit[index] && (tableInit[index] = true)):
                var pageArgs = {
                    pageIndex: 1,
                    pageSize: 20,
                    isLoading: false,
                    loadData: function(evt) {
                        if (2 == changeDetailTable.index && 0 == changeCommentTable.index) {
                            var that = this;
                            if (that.isLoading) {
                                return;
                            }
                            that.isLoading = true;
                            $('<li id="loading_element_0" class="loading_element" style="position:relative;"><span class="loading_icon">&nbsp;</span></li>').appendTo($('#list_comments_0'));
                            load_comments({
                                type: "1",
                                GoodsId: APP.goodsId,
                                pageIndex: that.pageIndex
                            }, function(res) {
                                that.isLoading = false;
                                if (0 == res.Status) {
                                    that.pageIndex++;
                                    $(iTemplate.makeList(TPL, res.Data)).replaceAll($("#loading_element_0"));
                                } else {
                                    alert(res.message);
                                }
                            });
                        }
                    }
                };
                var myScroll = new scrollEvt(pageArgs).start();
                pageArgs.loadData();
                break;
            case (1 == index && !tableInit[index] && (tableInit[index] = true)):
                var pageArgs = {
                    pageIndex: 1,
                    pageSize: 20,
                    isLoading: false,
                    loadData: function(evt) {
                        if (2 == changeDetailTable.index && 1 == changeCommentTable.index) {
                            var that = this;
                            if (that.isLoading) {
                                return;
                            }
                            that.isLoading = true;
                            $('<li id="loading_element_1" class="loading_element" style="position:relative;"><span class="loading_icon">&nbsp;</span></li>').appendTo($('#list_comments_1'));
                            load_comments({
                                type: "2",
                                GoodsId: APP.goodsId,
                                pageIndex: that.pageIndex
                            }, function(res) {
                                that.isLoading = false;
                                if (0 == res.Status) {
                                    that.pageIndex++;
                                    $(iTemplate.makeList(TPL, res.Data)).replaceAll($("#loading_element_1"));
                                } else {
                                    alert(res.message);
                                }
                            });
                        }
                    }
                };
                var myScroll = new scrollEvt(pageArgs).start();
                pageArgs.loadData();
                break;
            case (2 == index && !tableInit[index] && (tableInit[index] = true)):
                var pageArgs = {
                    pageIndex: 1,
                    pageSize: 20,
                    isLoading: false,
                    loadData: function(evt) {
                        if (2 == changeDetailTable.index && 2 == changeCommentTable.index) {
                            var that = this;
                            if (that.isLoading) {
                                return;
                            }
                            that.isLoading = true;
                            $('<li id="loading_element_2" class="loading_element" style="position:relative;"><span class="loading_icon">&nbsp;</span></li>').appendTo($('#list_comments_2'));
                            load_comments({
                                type: "3",
                                GoodsId: APP.goodsId,
                                pageIndex: that.pageIndex
                            }, function(res) {
                                that.isLoading = false;
                                if (0 == res.Status) {
                                    that.pageIndex++;
                                    $(iTemplate.makeList(TPL, res.Data)).replaceAll($("#loading_element_2"));
                                } else {
                                    alert(res.message);
                                }
                            });
                        }
                    }
                };
                var myScroll = new scrollEvt(pageArgs).start();
                pageArgs.loadData();
                break;
            default:

                break;
        }

        $("#nav_comments li, #list_comments .list_comments").removeClass("on");
        $("#nav_comments li:nth-of-type(" + (index + 1) + "), #list_comments .list_comments:nth-of-type(" + (index + 1) + ")").addClass("on");
        return changeCommentTable;
    }
    return changeCommentTable;
})();

var _count = $('input[name=total_count]').val();
//$('.specs dd').on('click', function() {
//    $(this).addClass('selected').siblings('dd').removeClass('selected');
//});


$(function() {
    $("#tabs1 a").click(function(e) {
        $(this).tab('show');
    });
});
// $(document).ready(function() {
//     var div_nav_fixed = document.getElementById('tabs1');
//     window.onscroll = function() {
//         var fiexdtop = div_nav_fixed.offsetTop;
//         var top = document.documentElement.scrollTop || document.body.scrollTop;
//         if (top > fiexdtop) {
//             $('#tabs1').addClass("fixed");
//         } else {
//             $('#tabs1').removeClass("fixed");
//         }
//     };
// });
